Completed
Push — master ( db83bf...a5d64d )
by Rafael S.
07:49
created

NumberBuffer.getFPParser_   A

Complexity

Conditions 3

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
/*
2
 * Copyright (c) 2017-2018 Rafael da Silva Rocha.
3
 *
4
 * Permission is hereby granted, free of charge, to any person obtaining
5
 * a copy of this software and associated documentation files (the
6
 * "Software"), to deal in the Software without restriction, including
7
 * without limitation the rights to use, copy, modify, merge, publish,
8
 * distribute, sublicense, and/or sell copies of the Software, and to
9
 * permit persons to whom the Software is furnished to do so, subject to
10
 * the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be
13
 * included in all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
 *
23
 */
24
25
/**
26
 * @fileoverview Sserialize and deserialize integers and floats.
27
 * @see https://github.com/rochars/byte-data
28
 */
29
30
import { IEEE754Buffer } from 'ieee754-buffer';
31
import { TwosComplementBuffer } from 'twos-complement-buffer';
32
import { UintBuffer } from 'uint-buffer';
33
34
/**
35
 * A class to pack and unpack integers and floating-point numbers.
36
 * Signed integers are two's complement.
37
 * Floating-point numbers are IEEE 754 standard.
38
 */
39
export default class NumberBuffer {
40
  
41
  /**
42
   * Read one number from a byte buffer.
43
   * @param {number} bits The number of bits of the number.
44
   * @param {boolean} fp Tue for floating-point numbers.
45
   * @param {boolean} signed True for signed numbers.
46
   * @throws {Error} If the type definition is not valid.
47
   */
48
  constructor(bits, fp, signed) {
49
    /** @type {TwosComplementBuffer|UintBuffer|IEEE754Buffer} */
50
    let parser;
51
    if (fp) {
52
      validateFloatType(bits);
53
      parser = this.getFPParser_(bits);
54
    } else {
55
      validateIntType(bits);
56
      parser = signed ? new TwosComplementBuffer(bits) : new UintBuffer(bits);
57
    }
58
    /** @type {TwosComplementBuffer|UintBuffer|IEEE754Buffer} */
59
    this.parser = parser;
60
    /** @type {number} */
61
    this.offset = Math.ceil(bits / 8);
62
  }
63
  
64
  /**
65
   * Read one number from a byte buffer.
66
   * @param {!Uint8Array|!Array<number>} buffer An array of bytes.
67
   * @param {number=} index The index to read.
68
   * @return {number} The number.
69
   * @throws {Error} On overflow.
70
   */
71
  unpack(buffer, index=0) {
72
    return this.parser.unpack(buffer, index);
73
  }
74
75
  /**
76
   * Write one number to a byte buffer.
77
   * @param {!Uint8Array|!Array<number>} buffer An array of bytes.
78
   * @param {number} num The number.
79
   * @param {number=} index The index being written in the byte buffer.
80
   * @return {number} The next index to write on the byte buffer.
81
   * @throws {Error} If num is NaN.
82
   * @throws {Error} On overflow.
83
   */
84
  pack(buffer, num, index=0) {
85
    return this.parser.pack(buffer, num, index);
86
  }
87
88
  /**
89
   * Return a instance of IEEE754Buffer.
90
   * @param {number} bits The number of bits.
91
   * @return {IEEE754Buffer}
92
   * @private
93
   */
94
  getFPParser_(bits) {
95
    if (bits === 16) {
96
      return new IEEE754Buffer(5, 11);
97
    } else if(bits === 32) {
98
      return new IEEE754Buffer(8, 23);
99
    } else {
100
      return new IEEE754Buffer(11, 52);
101
    }
102
  }
103
}
104
105
/**
106
 * @type {string} The type definition error message.
107
 * @private
108
 */
109
const TYPE_ERR = 'Unsupported type';
110
111
/**
112
 * Validate the type definition of floating-point numbers.
113
 * @param {number} bits The number of bits.
114
 * @throws {Error} If the type definition is not valid.
115
 * @private
116
 */
117
function validateFloatType(bits) {
118
  if (!bits || bits !== 16 && bits !== 32 && bits !== 64) {
119
    throw new Error(TYPE_ERR + ': float, bits: ' + bits);
120
  }
121
}
122
123
/**
124
 * Validate the type definition of integers.
125
 * @param {number} bits The number of bits.
126
 * @throws {Error} If the type definition is not valid.
127
 * @private
128
 */
129
function validateIntType(bits) {
130
  if (!bits || bits < 1 || bits > 53) {
131
    throw new Error(TYPE_ERR + ': int, bits: ' + bits);
132
  }
133
}
134